package api

import (
	"api-gateway/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	ext := filepath.Ext(file.Filename)
	if ext != ".jpg" {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "格式不正确",
		})
		return
	}
	gef := "../../oss/oss" + file.Filename
	c.SaveUploadedFile(file, gef)
	pkg.Upload(file.Filename, gef)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": fmt.Sprintf("https://tao12 .oss-cn-shanghai.aliyuncs.com/" + file.Filename),
	})

}
